package com.mofang.activity.user;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.MofangCommonUpdateView;
import com.mofang.controller.UserController;
import com.mofang.utils.MoFangSession;
import com.pactera.framework.exception.IException;
import com.pactera.framework.util.ToastHelper;

public class UserChangeNameActivity extends BaseTitleViewActiviey implements
		OnClickListener {

	private EditText name_enter_edit;// 输入用户名
	private Button btn_user_setting_change_name;// 确定
	private UserController userController;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_user_change_name);
		super.onCreate(savedInstanceState);
		setNavigationTitle("修改姓名");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		userController = new UserController();

	}

	@Override
	protected void initView() {
		super.initView();
		name_enter_edit = (EditText) findViewById(R.id.user_change_name_enter_edit);
		btn_user_setting_change_name = (Button) findViewById(R.id.btn_user_setting_change_name);
	}
	@Override
	protected void setListener() {
		super.setListener();
		btn_user_setting_change_name.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btn_user_setting_change_name:
			ToastHelper.getInstance().showShortMsg(
					name_enter_edit.getText().toString());
			String phone=MoFangSession.getAccount();
			String newName=name_enter_edit.getText().toString();
			userController.changeUserName(new ChangeUserNameUpdateView(
						mContext), phone, newName);
			// 确定
			// intent = new Intent(getApplicationContext(),
			// UserSettingActivity.class);
			// startActivity(intent);
			// finish();
			break;

		}
	}
	private class ChangeUserNameUpdateView extends MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public ChangeUserNameUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(UserChangeNameActivity.this, null,
					getResources().getString(R.string.handle_in_process), true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			//result=false;
			progressDialog.dismiss();
			if (result) {
				ToastHelper.getInstance().showLongMsg(getResources().getString(R.string.handle_success));
			} else {
				viewHelper.showBTNDialog(getResources().getString(R.string.handle_failure));
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}
}
